package com.lakeworks.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lakeworks.common.core.domain.AjaxResult;
import com.lakeworks.common.core.text.Convert;
import com.lakeworks.common.domain.ticket.TktClaims;
import com.lakeworks.common.domain.ticket.TktClaimsRecord;
import com.lakeworks.common.domain.ticket.TktTicket;
import com.lakeworks.common.exception.response.ResponseException;
import com.lakeworks.common.utils.SecurityUtils;
import com.lakeworks.mapper.TktClaimsMapper;
import com.lakeworks.mapper.TktClaimsRecordMapper;
import com.lakeworks.mapper.TktTicketMapper;
import com.lakeworks.service.ITktClaimsService;
import com.lakeworks.service.api.MerchantAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 赔偿单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-16
 */
@Service
public class TktClaimsServiceImpl implements ITktClaimsService 
{
    @Resource
    private TktClaimsMapper tktClaimsMapper;

    @Resource
    private TktTicketMapper tktTicketMapper;

    @Resource
    private TktClaimsRecordMapper tktClaimsRecordMapper;

    @Resource
    private MerchantAsync merchantAsync;

    /**
     * 查询赔偿单
     * 
     * @param id 赔偿单主键
     * @return 赔偿单
     */
    @Override
    public TktClaims selectTktClaimsById(Long id)
    {
        return tktClaimsMapper.selectTktClaimsById(id);
    }

    /**
     * 查询赔偿单列表
     *
     * @param tktClaims 赔偿单
     * @return 赔偿单
     */
    @Override
    public PageInfo<TktClaims> selectTktClaimsList(TktClaims tktClaims) {
        PageHelper.startPage(tktClaims.getPageNum(),tktClaims.getPageSize());
        List<TktClaims> claims = tktClaimsMapper.selectTktClaimsList(tktClaims);
        PageInfo<TktClaims> pageInfo = new PageInfo<>(claims);
        pageInfo.setList(claims.stream().peek(c -> c.setTId(Optional.ofNullable(tktTicketMapper.selectTktTicketBychannelId(c.getChannelId(), c.getTicketId())).map(TktTicket::getId).orElse(null))).toList());
        return pageInfo;
    }

    /**
     * 新增赔偿单
     * 
     * @param tktClaims 赔偿单
     * @return 结果
     */
    @Override
    public int insertTktClaims(TktClaims tktClaims)
    {
        return tktClaimsMapper.insertTktClaims(tktClaims);
    }

    /**
     * 修改赔偿单
     * 
     * @param tktClaims 赔偿单
     * @return 结果
     */
    @Override
    public int updateTktClaims(TktClaims tktClaims)
    {
        return tktClaimsMapper.updateTktClaims(tktClaims);
    }

    /**
     * 批量删除赔偿单
     * 
     * @param ids 需要删除的赔偿单主键
     * @return 结果
     */
    @Override
    public int deleteTktClaimsByIds(String ids)
    {
        return tktClaimsMapper.deleteTktClaimsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除赔偿单信息
     * 
     * @param id 赔偿单主键
     * @return 结果
     */
    @Override
    public int deleteTktClaimsById(Long id)
    {
        return tktClaimsMapper.deleteTktClaimsById(id);
    }

    @Override
    public TktClaims selectTktClaimsByChId(Long channelId,Long chClaimsId) {
        return tktClaimsMapper.selectTktClaimsByChId(channelId,chClaimsId);
    }

    @Override
    public AjaxResult addRecord(TktClaimsRecord tktClaimsRecord) {
        TktClaims tktClaims = selectTktClaimsById(tktClaimsRecord.getClaimsId());
        if (tktClaims.getAmount().compareTo(tktClaimsRecord.getFtyAmount().add(tktClaimsRecord.getPlatformAmount())) != 0)
            throw new ResponseException("金额有误");

        tktClaimsRecord.setType(0);
        tktClaimsRecord.setCreateBy(SecurityUtils.getUserName());
        tktClaimsRecord.setModifyBy(SecurityUtils.getUserName());
        int num = tktClaimsRecordMapper.insertSelective(tktClaimsRecord);
        
        TktClaims build = TktClaims.builder()
                .reFtyAmount(tktClaimsRecord.getFtyAmount())
                .rePlatformAmount(tktClaimsRecord.getPlatformAmount())
                .reconsiderTime(LocalDateTime.now())
                .reconsiderComment(tktClaimsRecord.getComment())
                .reconsiderStatus("UNDERREVIEW")
                .build();
        build.setId(tktClaims.getId());
        updateTktClaims(build);

        tktClaimsRecord.setClaimsId(tktClaims.getChClaimsId());
        if(num != 0){
//            List<OprPickup> pickups = oprPickupMapper.selectOprPickupList(OprPickup.builder().orderNo(tktClaimsRecord.getOrderNo()).build());
//            if(!CollectionUtils.isEmpty(pickups)){
            merchantAsync.reconsider(tktClaimsRecord, tktClaims.getChannelId());
//            }
        }
        return AjaxResult.success(num);
    }

    @Override
    public int insertSelective(TktClaimsRecord record) {
        return tktClaimsRecordMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(TktClaimsRecord record) {
        return tktClaimsRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public AjaxResult getRecord(Long claimsId) {
        return AjaxResult.success(tktClaimsRecordMapper.selectRecordList(TktClaimsRecord.builder().claimsId(claimsId).build()));
    }

}
